<?php
/**
 * Created by PhpStorm.
 * User: DongYao
 * Date: 2017/12/15
 * Time: 下午2:56
 */

namespace XmtApp\Modules\Admin\Controllers\Api;


use App\Support\Facades\ConfigSet;
use Illuminate\Http\Request;
use XmtApp\Modules\Admin\Controllers\Controller;
use Artisan;

class ConfigController extends Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 设置系统参数
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function setSystem(Request $request)
    {
        // 提交的设置参数
        $configs = $request->get('configs', []);

        $new_configs = [];
        foreach ($configs as $item) {
            $key = $item['key'];
            $value = $item['value'];
            $description = $item['description'];

            if (!empty($key) && !empty($value) && !empty($description)) {
                array_push($new_configs, [
                   'key' => $key,
                   'value' => $value,
                   'description' => $description
                ]);
            }

            ConfigSet::save('system', $new_configs);
        }

        // 清除config缓存
        Artisan::call('config:clear');

        return response()->json(ajax_return(true, '设置成功', config('system', [])));
    }

    /**
     * 获取系统参数
     * @return \Illuminate\Http\JsonResponse
     */
    public function getSystem()
    {
        $config = config('system', []);

        return response()->json(ajax_return(true, '获取配置信息成功', $config));
    }
}